#include "cplaylist.h"
#include <windows.h>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QHBoxLayout>

CPlayList::CPlayList(QWidget *parent) : QWidget(parent)
, _list(new unordered_set<QString>())
{
    setFixedWidth(300);
    setVisible(false);
    initPlaylist();
}

//加载视频文件
void CPlayList::initPlaylist()
{
    _playlistwidget = new QListWidget(this);

    _playlistwidget->setFixedWidth(300);//限制宽度避免占用过多空间
    _playlistwidget->setStyleSheet(
        "QListWidget { background: #2C3E50; border: none; }"
        "QListWidgetItem { padding: 8px; color: white; }"
        "QListWidgetItem:hover { background: #34495E; }"
    );

    QWidget *titleWidget = new QWidget(this);
    titleWidget->setFixedWidth(300);//限制宽度避免占用过多空间
    titleWidget->setStyleSheet("background-color:rgb(54,54,54)");

    _ptitleTextLabel = new QLabel(titleWidget);
    _ptitleTextLabel->setText("播放列表");
    _ptitleTextLabel->setFixedWidth(120);
    _ptitleTextLabel->setStyleSheet(
            "QLabel{font-family: Microsoft YaHei;"
                "font-size:18px;"
                "color:#BDC8E2;background-color:rgb(54,54,54);}");

    _pcloseButton = new QPushButton(titleWidget);
    _pcloseButton->setFixedSize(32, 32);
    _pcloseButton->setStyleSheet(
            "QPushButton{background-image:url(:/resources/titlebar/close.svg);border:none}"
            "QPushButton:hover{"
                    "background-color:rgb(99, 99, 99);"
                    "background-image:url(:/resources/titlebar/close_hover.svg);border:none;}");

    QVBoxLayout *Layout = new QVBoxLayout(this);
    Layout->setContentsMargins(0, 0, 0, 0);
    QHBoxLayout * phLayout = new QHBoxLayout(titleWidget);
    phLayout->setContentsMargins(5,5,5,5);
    phLayout->addWidget(_ptitleTextLabel);
    phLayout->addStretch();//添加一个弹簧

    phLayout->addWidget(_pcloseButton);
    Layout->addWidget(titleWidget);
    Layout->setSpacing(0); // 设置间距为0
    Layout->addWidget(_playlistwidget);



    connect(_pcloseButton, &QPushButton::clicked, this, &CPlayList::closelist);
    connect(_playlistwidget, &QListWidget::itemDoubleClicked, this, &CPlayList::playlist);
}

void CPlayList::showlist(QString filePath)
{
    //QFileInfo fileInfo(filePath);
    QListWidgetItem *item = new QListWidgetItem(filePath);
    item->setData(Qt::UserRole, filePath); // 存储完整路径

    _playlistwidget->addItem(item);

}

int CPlayList::getIndex(QListWidgetItem *item)
{
    return _playlistwidget->row(item); // 设置新的当前项
}

void CPlayList::mousePressEvent(QMouseEvent* event)
{
    if (ReleaseCapture())
    {
        QWidget* pWindow = this->window();//返回指向标题栏所属顶层窗口的指针。
        if (pWindow->isTopLevel()) //判断该窗口是否为顶层窗口（即独立窗口）
        {
            SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
}

void CPlayList::updateCurrentPlayingItem(int idx)
{
    QListWidgetItem *item;
    // 将旧的当前项取消高亮
    if(_oldIdx >= 0){
        item = _playlistwidget->item(_oldIdx);
        item->setBackground(Qt::transparent);
    }

    if(idx >= 0){
        // 将新的当前项高亮
        item = _playlistwidget->item(idx);
        item->setBackground(QColor("#4FACFE")); // 蓝色高亮
        _playlistwidget->setCurrentItem(item);
    }
    _oldIdx = idx;
}




